/*
 * Copyright (c) 2012, Markus Weber All rights reserved.
 * 
 * Redistribution and use in source and binary forms, with or without modification, are
 * permitted provided that the following conditions are met:
 * 
 * Redistributions of source code must retain the above copyright notice, this list of
 * conditions and the following disclaimer. Redistributions in binary form must reproduce the
 * above copyright notice, this list of conditions and the following disclaimer in the
 * documentation and/or other materials provided with the distribution.
 * 
 * Neither the name of the author nor the names of its contributors may be used to endorse or
 * promote products derived from this software without specific prior written permission.
 * 
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS
 * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
 * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR
 * TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
 * EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 * 
 */
package de.dfki.graph.index;

import java.util.HashMap;
import java.util.Map;

import de.dfki.graph.Vertex;

/**
 * Result of the graph matching search.
 *
 * @author Markus Weber
 *
 */
public class SearchResult implements Comparable<SearchResult> {

    /**
     * Calculated similarity.
     */
    protected double similarity;
    /**
     * Associated graph plan.
     */
    protected String result;
    /**
     * Matching of the entities
     */
    public Map<Vertex<?>, Vertex<?>> matching = new HashMap<Vertex<?>, Vertex<?>>();

    /**
     * Creates a search result.
     *
     * @param similarity
     * @param result
     */
    public SearchResult(double similarity, String result) {
        this(similarity, result, new HashMap<Vertex<?>, Vertex<?>>());
    }

    public SearchResult() {
        this(1.0, null, new HashMap<Vertex<?>, Vertex<?>>());
    }

    public SearchResult(double similarity, String result,
            Map<Vertex<?>, Vertex<?>> matching) {
        super();
        this.similarity = similarity;
        this.result = result;
        this.matching = matching;
    }

    /*
     * (non-Javadoc)
     *
     * @see java.lang.Comparable#compareTo(java.lang.Object)
     */
    @Override
    public int compareTo(SearchResult o) {
        if (o.similarity > this.similarity) {
            return 1;
        }
        if (this.result.equals(o.result)) {
            return 0;
        }
        return -1;
    }

    /**
     * Matches the query to graph node.
     *
     * @param query
     * @param graph
     */
    public void match(Vertex<?> query, Vertex<?> graph) {
        matching.put(query, graph);
    }

    /**
     * @return the similarity
     */
    public double getSimilarity() {
        return similarity;
    }

    /**
     * @return the result
     */
    public String getResult() {
        return result;
    }

    /*
     * (non-Javadoc)
     *
     * @see java.lang.Object#toString()
     */
    @Override
    public String toString() {
        StringBuilder builder = new StringBuilder();
        builder.append("SearchResult [");
        if (result != null) {
            builder.append("result=");
            builder.append(result);
            builder.append(", ");
        }
        builder.append("similarity=");
        builder.append(similarity);
        builder.append("]");
        return builder.toString();
    }

    /**
     * Clones the result.
     *
     * @return
     */
    public SearchResult cloneResult() {
        return new SearchResult(this.similarity, this.result);
    }

    /**
     * Clones the result.
     *
     * @param s
     * @return
     */
    public SearchResult cloneResult(String s) {
        return new SearchResult(this.similarity, s, this.matching);
    }
}
